#include "ThreadPool.h"

int getAbsolutePath(int id_now,const char* path,const char* username,MYSQL* mysql){
    char sql[4096]={0};
    char path_usr[4096]={0};
    strcpy(path_usr,path);
    if(strlen(path_usr)==0){
        return id_now;
    }
    if(path_usr[0]=='/'){
        id_now=1;
    }
    char* token;
    token=strtok(path_usr,"/");
    while(token){
        if(strcmp(token,"..")==0){
            bzero(sql,sizeof(sql));
            sprintf(sql,"select pre_id from virtual_file where user='%s' and id=%d ;",username,id_now);
            int ret=mysql_query(mysql,sql);
            MYSQL_QUERY_CHECK(ret,mysql);
  
            MYSQL_RES* result=mysql_store_result(mysql);
            MYSQL_ROW row=mysql_fetch_row(result);
            int resu = atoi(row[0]);
            if (resu != -1) {
                id_now = resu;
            }
        }
        else if(strcmp(token,".")==0){

        }
        else{
            bzero(sql,sizeof(sql));
            sprintf(sql,"select id from virtual_file where user='%s' and pre_id=%d and name='%s';",username,id_now,token);
            int ret=mysql_query(mysql,sql);
            MYSQL_QUERY_CHECK(ret,mysql);

            MYSQL_RES* result=mysql_store_result(mysql);
            MYSQL_ROW row=mysql_fetch_row(result);
            unsigned long rows_num=mysql_num_rows(result);
            if(rows_num==(unsigned long)0){
                return -1;
            }
            else{

                int resu=atoi(row[0]);
                id_now=resu;
            }
        }
        token=strtok(NULL,"/");
    }
    return id_now;
}
